package Bi_Tree.symmetry_Bitree;

import Bi_Tree.TreeNode;

import java.util.Stack;

public class lc572 {
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
       /**
       * @Description: 判断一棵树是否为另一棵树的子树  遍历root每一个节点将其与subRoot比较
       * @Param: [root, subRoot]
       * @return: boolean
       * @Author: lsy
       * @Date: 2024/5/6 0006
       */
       Stack<TreeNode> stack=new Stack<>();
       TreeNode node=root;
       if(root==null&&subRoot==null){
           return true;
       } else if (root!=null&&subRoot==null) {
           return false;
       } else if (root==null&&subRoot!=null) {
           return false;
       }
        while (node!=null||!stack.isEmpty()){
           if(node!=null){
               stack.push(node);
               node=node.left;
           }else{
               node=stack.pop();
               if(issanme(node,subRoot)){
                   return true;
               }
               node=node.right;
           }
       }
        return false;

    }

    public boolean issanme(TreeNode node,TreeNode subRoot){
        if(node==null&&subRoot==null){
            return true;
        } else if (node!=null&&subRoot==null) {
            return false;
        } else if (node==null&&subRoot!=null) {
            return false;
        } else if (node.val!=subRoot.val) {
            return false;
        }

        boolean a=issanme(node.left,subRoot.left);
        boolean b=issanme(node.right,subRoot.right);
        return a&&b;
    }
}
